import axios from "axios";
import {MyUrl} from "./Constant.ts";

let user
export function hook_getUser() {
    user = JSON.parse(sessionStorage.getItem("user"));
    return user
}

export async function hook_updateUserToStorage() {
    await hook_findUserById(hook_getUser().id, (value) => {
        console.log(value)
        console.log("更新完成")
        sessionStorage.setItem("user", JSON.stringify(value))
    })
}


let currentEditSong
export function hook_currentEditSong() {
    currentEditSong = JSON.parse(sessionStorage.getItem("currentEditSong"));
    return currentEditSong
}

let favorite
export function hook_getFavorite() {
    favorite = JSON.parse(sessionStorage.getItem("favorite"));
    return favorite
}

export async function hook_findUserById(id: number,cb) {
    await axios.get(MyUrl.USER_findUserById, {
        params: {
            id: id,
        }
    }).then(res => {
        console.log(res.data)
        cb(res.data.data)
    }).catch(error => {
        console.error("查找用户失败：" + error)
    })
}

export async function hook_findUserByPlaylistId(id: number) {
    return await axios.get(MyUrl.USER_findUserByPlaylistId, {
        params: {
            playlistId: id,
        }
    }).then(res => {
        console.log(res.data)
        return res.data.data
    }).catch(error => {
        console.error("查找用户失败：" + error)
    })
}


export function hook_getFavoriteByUserId(userId: number) {
    return axios.get(MyUrl.FAVORITE_getFavoriteByUserId, {
        params: {
            userId: userId,
        }
    }).then(res => {
        console.log(res.data)
        if (res.data.flag) {
            return res.data.data
        }
    }).catch(error => {
        console.error("查找Favorite失败：" + error)
    })
}

export function hook_findUserByKeyword(keyword: string) {
    return  axios.get(MyUrl.USER_findUserByKeyword, {
        params: {
            keyword: keyword,
        }
    }).then(res => {
        console.log(res.data)
        if (res.data.flag) {
            return res.data.data
        }
    }).catch(error => {
        console.error("查找用户失败：" + error)
    })
}


export function hook_getUserPageByKeyword(keyword: string,pageIndex:number) {
    return  axios.get(MyUrl.USER_getUserPageByKeyword, {
        params: {
            keyword: keyword,
            pageIndex:pageIndex
        }
    }).then(res => {
        console.log(res.data)
        if (res.data.flag) {
            return res.data.data
        }
    }).catch(error => {
        console.error("查找用户失败：" + error)
    })
}

export function hook_updateUser(user) {
    return axios.put(MyUrl.USER_updateUser, JSON.stringify(user), {
        headers: {
            "Content-Type": "application/json"
        }
    }).then(res => {
        return res.data
    }).catch(error => {
        console.log(error)
    })
}

export function hook_forgetPasswordCreateCodeByEmail(email) {
    return axios.post(MyUrl.USER_forgetPasswordCreateCodeByEmail, {
        email: email
    }).then(res => {
        console.log(res.data)
        return res.data
    }).catch(error => {
        console.log(error)
    })
}

export function hook_forgetPasswordCheckCode(email, code) {
    return axios.post(MyUrl.USER_forgetPasswordCheckCode, {
        email: email,
        code: code,
    }).then(res => {
        console.log(res.data)
        return res.data
    }).catch(error => {
        console.log(error)
    })
}

export function hook_checkEmail(email) {
    return axios.post(MyUrl.USER_checkEmail, {
        email: email
    }).then(res => {
        console.log(res.data)
        return res.data
    }).catch(error => {
        console.log(error)
    })
}

export function hook_editPasswordByEmail(email, password) {
    return axios.post(MyUrl.USER_editPasswordByEmail, {
        email: email,
        password: password,
    }).then(res => {
        console.log(res.data)
        return res.data
    }).catch(error => {
        console.log(error)
    })
}




